<template>
  <div class="search">
    <div class="searchbox">
      <router-link
        tag="span"
        to="/"
        class="searchicon iconfont icon-zuojiantou"
      ></router-link>
      <div class="content">
        <span class="searchicon iconfont icon-sousuo"></span>
        <input type="text" @keyup.enter="searchResult(value)" v-model="value" />
        <span class="delicon iconfont icon-cuowuguanbiquxiao-yuankuang"></span>
      </div>
      <p @click="searchResult(value)">搜索</p>
    </div>

    <div class="hotsearch" v-if="!value">
      <p class="title">热门搜索</p>
      <div class="list">
        <div
          class="item"
          v-for="(hotitem, index) in hotSearch"
          :key="index"
          @click="searchResult(hotitem.title)"
        >
          {{ hotitem.title }}
        </div>
      </div>
    </div>

    <div class="searchhistory" v-if="!value">
      <div class="title">
        <span>搜索历史</span>
        <span class="iconfont icon-shanchu" @click="deleteAll"></span>
      </div>
      <div class="list">
        <div
          class="item"
          v-for="(history, index) in seacrchhistory"
          :key="index"
        >
          <span class="historyicon iconfont icon-lishijilu"></span>
          <p @click="searchResult(history)">{{ history }}</p>
          <span
            class="delete iconfont icon-cuowuguanbiquxiao"
            @click="deletes(index)"
          ></span>
        </div>
      </div>
    </div>

    <div class="searchlist" v-if="value">
      <div class="title">搜索 {{ value }} 的人都在看</div>
      <!-- <van-loading size="39px" vertical type="spinner" 
      >加载中...</van-loading
    > -->
      <div class="list">
        <div class="item" v-for="item in searchResultList" :key="item.id"  @click="gotoPassage(item.id)">
          <div class="img">
            <van-image :src="item.vertical_cover">
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
            </van-image>
          </div>
          <div class="text">
            <h3 v-html="item.title"></h3>
            <!-- <p></p> -->
            <p class="author" v-html="item.author_name[0]"></p>
            <div class="replace">
              <span v-for="(replace,index) in item.styles" :key="index">{{replace}}</span>
              
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      hotSearch: [],
      searchResultList: [],
      value: "",
      seacrchhistory: JSON.parse(localStorage.getItem("seacrchhistory")) || [],
    };
  },
  created() {
    //热门搜索
    this.axios.get("/SearchRecommend?num=3").then((res) => {
      // console.log(res);
      this.hotSearch = res.data.data;
      // console.log(this.hotSearch);
    });
  },
  methods: {
    //搜索结果
    searchResult(value) {
      this.value = value;
      if (value) {
        this.axios.get(`/Search?keyWord=${value}`).then((res) => {
          // console.log(value);
          this.searchResultList = res.data.data.list;
        if(this.searchResultList.length==0){
              this.axios.get("/Search?keyWord=斗罗").then((res) =>{
                this.searchResultList = res.data.data.list;
          // console.log(this.searchResultList);
              })
        }
          // console.log(this.seacrchhistory);
          this.seacrchhistory.unshift(value);
          this.seacrchhistory = new Set(this.seacrchhistory);
          this.seacrchhistory = [...this.seacrchhistory];
        });
      }
    },
    //删除历史记录
    deletes(index) {
      // console.log(index);
      this.seacrchhistory.splice(index, 1);
    },

    deleteAll(){
      this.seacrchhistory = []
      localStorage.removeItem('seacrchhistory');
    },


    gotoPassage(comic_id) {
      this.$router.push({
        path: "/passage",
        query: {
          id: comic_id,
        },
      });
    },

  },
  watch: {
    seacrchhistory: {
      handler(newValue) {
        console.log(newValue);
        localStorage.setItem("seacrchhistory", JSON.stringify([...newValue]));
      },
      deep: true,
    },
    value: function (newValue) {
      // console.log(newValue);
      if (newValue == "") {
        this.resultlist = null;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.search {
  padding: 0px 12px 50px;
  .searchbox {
    padding: 10px 0px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .content {
      width: 80%;
      height: 30px;
      line-height: 30px;
      padding: 0px 30px;
      border-radius: 30px;
      position: relative;

      background-color: #f6f6f6;
      .searchicon {
        color: gray;
        position: absolute;
        top: 0;
        left: 10px;
      }
      input {
        width: 80%;
        padding-left: 5px;
        color: #333;
        border: none;
        background-color: #f6f6f6;
        outline: none;
        caret-color: red;
      }
      .delicon {
        position: absolute;
        top: 0;
        right: 10px;
        color: #bcbdbd;
      }
    }
  }
  .hotsearch {
    padding: 15px 0px 0px;
    .title {
      font-size: 12px;
      color: #666;
      margin-bottom: 10px;
    }
    .list {
      .item {
        display: inline-block;
        height: 32px;
        margin-right: 8px;
        margin-bottom: 8px;
        padding: 0 14px;
        font-size: 14px;
        line-height: 32px;
        color: #000;
        // border: 1px solid #d3d4da;
        border-radius: 32px;
        background-color: #f6f6f6;
      }
    }
  }
  .searchhistory {
    .title {
      font-size: 12px;
      color: #666;
      display: flex;
      justify-content: space-between;
    }
    .list {
      .item {
        position: relative;
        .historyicon {
          position: absolute;
          top: 15px;
          left: 10px;
          font-size: 15px;
          color: gray;
        }
        p {
          height: 45px;
          line-height: 45px;
          font-size: 14px;
          color: #333;
          margin-left: 35px;
        }
        .delete {
          position: absolute;
          top: 15px;
          right: 0px;
          font-size: 14px;
          color: gray;
        }
      }
    }
  }
  .searchlist {
    .title {
      font-size: 12px;
      color: #666;
    }
    .list {
      .item {
        display: flex;
        margin-top: 15px;
        .img {
          width: 80px;
          height: 110px;
          margin-right: 10px;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .text {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          h3 {
            width: 230px;
            height: 24px;
            line-height: 24px;
            font-size: 14px;
            color: #333;
            font-weight: bold;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          p {
            height: 22px;
            line-height: 22px;
            font-size: 12px;
            color: #666;
          }
          // .author {
          //   margin: 15px 0px 10px 0px;
          // }
          .replace {
            display: flex;
            span {
              // width: 36px;
              padding: 0 5px;
              height: 16px;
              line-height: 14px;
              text-align: center;
              font-size: 10px;
              color: #666;
              border: 1px solid #666;
              margin-right: 5px;
              border-radius: 10px;
            }
          }
        }
      }
    }
  }
  .van-loading--vertical {
    margin-top: 300px;
  }
}
</style>